package cn.gogoyc.org.basic.controller;

import cn.gogoyc.org.basic.dto.PhoneCodeDto;
import cn.gogoyc.org.basic.servie.VerifyCodeService;
import cn.gogoyc.org.basic.util.AjaxResult;
import cn.gogoyc.org.basic.util.VerifyCodeUtils;
import org.apache.ibatis.builder.BuilderException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 验证码接口
 * @author Yang
 * @date 2023 /2/7 16:43
 */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private VerifyCodeService verifyCodeService;

    /**
     * 获取图形验证码
     *
     * @param key the key
     * @return the image code
     */
    @GetMapping("/image/{key}")             //接收这个UUID
    public AjaxResult getImageCode(@PathVariable("key") String key) {
        try {
            //将这个UUID传给service进行处理后返回
            String base64Str = verifyCodeService.getImageCode(key);
            //返回状态对象并且设置obj的值,这个值就是进行处理过的图片的base64编码
            return AjaxResult.me().setObj(base64Str);
        } catch (BuilderException e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage(), false);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage(), false);
        }
    }

    /**
     * 获取手机验证码
     * @param phoneCodeDto the phone code dto
     * @return the phone code
     */
    @PostMapping("/phone")
    public AjaxResult getPhoneCode(@RequestBody PhoneCodeDto phoneCodeDto) {
        try {
            //将这个UUID传给service进行处理后返回
            verifyCodeService.getPhoneCode(phoneCodeDto);
            //返回状态对象并且设置obj的值,这个值就是进行处理过的图片的base64编码
            return AjaxResult.me();
        } catch (BuilderException e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage(), false);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage(), false);
        }
    }

    @PostMapping("/binderSmsCode")
    public AjaxResult getBinderSmsCode(@RequestBody PhoneCodeDto phoneCodeDto) {
        try {
            //将这个UUID传给service进行处理后返回
            verifyCodeService.getBinderSmsCode(phoneCodeDto);
            //返回状态对象并且设置obj的值,这个值就是进行处理过的图片的base64编码
            return AjaxResult.me();
        } catch (BuilderException e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage(), false);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("网络繁忙...请稍后再试..", false);
        }
    }


}
